﻿using Leap;
using System;
using System.Collections.Generic;

namespace PaintTheWay.TestOnly
{
    public class SingleListener : Listener
    {
        public override void OnInit(Controller cntrlr)
        {
            //Console.WriteLine("Initialized");
        }

        public override void OnConnect(Controller cntrlr)
        {
            //Console.WriteLine("Connected");
        }

        public override void OnDisconnect(Controller cntrlr)
        {
            //Console.WriteLine("Disconnected");
        }

        public override void OnExit(Controller cntrlr)
        {
            //Console.WriteLine("Exited");
        }

        private long currentTime;
        private long previousTime;
        private long timeChange;
        const int FramePause = 10000;
        public List<FingerPointStorage> fingerPoint = new List<FingerPointStorage>();

        //public override void OnFrame(Controller ctrl)
        //{
        //    //Get the current frame
        //    Frame currentFrame = ctrl.Frame();

        //    this.currentTime = currentFrame.Timestamp;
        //    this.timeChange = currentTime - previousTime;

        //    if (this.timeChange < 10000 || currentFrame.Hands.IsEmpty)
        //        return;

        //    //Get the first finger
        //    Finger finger = currentFrame.Fingers[0];

        //    //Get the closest screen that intercepts a ray projected from the finger
        //    Screen screen = ctrl.LocatedScreens.ClosestScreenHit(finger);

        //    //Get finger tip's velocity
        //    float tipVelocity = (int)finger.TipVelocity.Magnitude;

        //    /*only print info when velocity exceeds a certain threshold,
        //      so that the slightest movements won't continuously output
        //      data to the screen*/
        //    if (screen == null || !screen.IsValid || tipVelocity <= 25)
        //        return;
        //    else
        //    {

        //        Vector vector = screen.Intersect(finger, true);
        //        Hand hand = currentFrame.Hands.Frontmost; 

        //        //x and y positions are sometimes returned as NaN (e.g. if hand is partly obscured by screen)
        //        if (float.IsNaN(vector.x) || float.IsNaN(vector.y) || float.IsNaN(vector.z))
        //            return;

        //        float xScreenIntersect = vector.x;
        //        float yScreenIntersect = vector.y;
        //        float zScreenIntersect = hand.PalmPosition.z;

        //        float x = xScreenIntersect * screen.WidthPixels;
        //        float y = screen.HeightPixels - (yScreenIntersect * screen.HeightPixels);
        //        //float z = zScreenIntersect;
               
        //        if (fingerPoint.Count <= 0)
        //        {
        //            fingerPoint.Add(new FingerPointStorage(xScreenIntersect, yScreenIntersect,zScreenIntersect, 1));
        //        }
        //        else
        //        {
        //            fingerPoint[0].g_X = x;
        //            fingerPoint[0].g_Y = y;
        //            fingerPoint[0].g_Z = zScreenIntersect;
        //            fingerPoint[0].isActive = true;
        //            Console.WriteLine("leap x-axis: {0},y-axis: {1},z-axis: {2}", fingerPoint[0].g_X, fingerPoint[0].g_Y, fingerPoint[0].g_Z);
        //        }
        //    }
        //    previousTime = currentTime;
        //}

        public override void OnFrame(Controller cntrlr)
        {
            // Get the current frame.
            Frame currentFrame = cntrlr.Frame();

            currentTime = currentFrame.Timestamp;
            timeChange = currentTime - previousTime;
            int nbActiveFinger = 0;
            if (timeChange > FramePause)
            {
                if (!currentFrame.Hands.IsEmpty)
                {
                    // Get the first finger in the list of fingers
                    FingerList handFingers = currentFrame.Fingers;
                    // Get the closest screen intercepting a ray projecting from the finger
                    foreach (Finger finger in handFingers)
                    {

                        Screen screen = cntrlr.LocatedScreens.ClosestScreenHit(finger);

                        if (screen != null && screen.IsValid)
                        {
                            // Get the velocity of the finger tip
                            var tipVelocity = (int)finger.TipVelocity.Magnitude;
                            Hand hand = currentFrame.Hands.Frontmost;
                            // Use tipVelocity to reduce jitters when attempting to hold
                            // the cursor steady
                            if (tipVelocity > 25)
                            {
                                var xScreenIntersect = screen.Intersect(finger, true).x;
                                var yScreenIntersect = screen.Intersect(finger, true).y;
                                float zScreenIntersect = hand.PalmPosition.z;

                                if (xScreenIntersect.ToString() != "NaN")
                                {
                                    var x = (int)(xScreenIntersect * screen.WidthPixels);
                                    var y = (int)(screen.HeightPixels - (yScreenIntersect * screen.HeightPixels));

                                    if (fingerPoint.Count <= nbActiveFinger)
                                    {
                                        fingerPoint.Add(new FingerPointStorage(zScreenIntersect, xScreenIntersect, nbActiveFinger,true));
                                    }
                                    else
                                    {
                                        fingerPoint[nbActiveFinger].g_Forward = 1 + zScreenIntersect;
                                        fingerPoint[nbActiveFinger].g_LeftRight = x;
                                        fingerPoint[nbActiveFinger].g_NumFinger = nbActiveFinger;
                                        // fingerPoint[0].g_Z = zScreenIntersect;
                                        fingerPoint[nbActiveFinger].isActive = true;
                                        //Console.WriteLine("leap x-axis: {0},y-axis: {1},z-axis: {2}", fingerPoint[0].g_X, fingerPoint[0].g_Y, fingerPoint[0].g_Z);
                                    }
                                    nbActiveFinger++;
                                }
                            }
                        }
                    }
                    if (nbActiveFinger <= fingerPoint.Count)
                        for (int i = nbActiveFinger; i < fingerPoint.Count; i++)
                            fingerPoint[i].isActive = false;
                }
                previousTime = currentTime;
            }
        }
    }
}